package com.youbug.mall.member_ums.feign.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youbug.common.exception.BizCodeEnum;
import com.youbug.common.to.SocialUserTo;
import com.youbug.common.to.UmsMemberTo;
import com.youbug.mall.auth.vo.UserLoginVo;
import com.youbug.mall.member_ums.entity.UmsMember;
import com.youbug.mall.member_ums.exception.PhoneException;
import com.youbug.mall.member_ums.exception.UsernameException;
import com.youbug.mall.member_ums.utils.MyBCryptPasswordEncoder;
import com.youbug.mall.member_ums.vo.UserRegisterVo;
import com.youbug.common.vo.R;
import com.youbug.mall.auth.feign.IAuthMemberFeignService;
import com.youbug.mall.member_ums.service.IUmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthMemberFeignServiceImpl implements IAuthMemberFeignService {

    @Autowired
    private IUmsMemberService memberService;

    @Override
    public R<String> register(com.youbug.mall.auth.vo.UserRegisterVo vo) {
        UserRegisterVo registerVo = new UserRegisterVo();

        BeanUtils.copyProperties(vo, registerVo);
        try {
            memberService.register(registerVo);
        } catch (PhoneException e) {
            return R.fail(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode().intValue(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        } catch (UsernameException e) {
            return R.fail(BizCodeEnum.USER_EXIST_EXCEPTION.getCode().intValue(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }
        return R.ok();
    }

    @Override
    public R<UmsMemberTo> login(UserLoginVo vo) {

        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();

        //1、去数据库查询 SELECT * FROM ums_member WHERE username = ? OR mobile = ?
        UmsMember member = memberService.getOne(new QueryWrapper<UmsMember>()
                .eq("username", loginacct).or().eq("mobile", loginacct));

        if (member != null) {
            //获取到数据库里的password
            String password1 = member.getPassword();
            MyBCryptPasswordEncoder passwordEncoder = new MyBCryptPasswordEncoder();
            //进行密码匹配
            boolean matches = passwordEncoder.matches(password, password1);
            if (matches) {
                //登录成功
                UmsMemberTo memberTo = new UmsMemberTo();

                BeanUtils.copyProperties(member,memberTo);

                return R.ok(memberTo);
            }else{
                //登录失败
                return R.fail(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode().intValue(),
                        BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
            }
        }else{
            //登录失败
            return R.fail(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode().intValue(),
                    BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
    }

    @Override
    public R<UmsMemberTo> oauthLogin(SocialUserTo socialUser) {

        UmsMember member = null;
        try {
            member = memberService.login(socialUser);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode().intValue(),
                    BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
        if (member != null) {
            UmsMemberTo memberTo = new UmsMemberTo();
            BeanUtils.copyProperties(member, memberTo);
            return R.ok(memberTo);
        } else {
            return R.fail(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode().intValue(),
                    BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }
    }
}
